using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.TaskManage.Dto;
using ZR.Model.TaskManage;
using ZR.Repository;
using ZR.Service.TaskManage.ITaskManageService;

namespace ZR.Service.TaskManage
{
    /// <summary>
    /// AGV任务Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ITaskAgvService), ServiceLifetime = LifeTime.Transient)]
    public class TaskAgvService : BaseService<TaskAgv>, ITaskAgvService
    {
        /// <summary>
        /// 查询AGV任务列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<TaskAgvDto> GetPageList(TaskAgvQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<TaskAgv, TaskAgvDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="TaskNo"></param>
        /// <returns></returns>
        public TaskAgv GetInfo(long TaskNo)
        {
            var response = Queryable()
                .Where(x => x.TaskNo == TaskNo)
                .First();

            return response;
        }

        /// <summary>
        /// 添加AGV任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public TaskAgv AddTaskAgv(TaskAgv model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改AGV任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateTaskAgv(TaskAgv model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<TaskAgv> QueryExp(TaskAgvQueryDto parm)
        {
           var predicate = Expressionable.Create<TaskAgv>();

            predicate = predicate.AndIF(parm.TaskNo != null, it => it.TaskNo == parm.TaskNo);
            predicate = predicate.AndIF(parm.TaskComtype != null, it => it.TaskComtype == parm.TaskComtype);
            predicate = predicate.AndIF(parm.TaskWkstatus != null, it => it.TaskWkstatus == parm.TaskWkstatus);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PlanCode), it => it.PlanCode == parm.PlanCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PackageCode), it => it.PackageCode == parm.PackageCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PickId), it => it.PickId == parm.PickId);

            return predicate;
        }
    }
}